package com.example.test.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.renn.rennsdk.RennResponse;
import com.tencent.weibo.sdk.android.model.ModelResult;


public class RennJsonAnalyzer {

	public static List<HashMap<String, String>> OneWeiboJsonToHasArr(ModelResult result)
	{
		if(!result.isSuccess())
		{
			return null;
		}
		
        try {
        	JSONObject object =(JSONObject) result.getObj();
			List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map =new HashMap<String, String>();
			JSONObject firstObj =object.getJSONObject("data");
			String beginDate=firstObj.getString("timestamp")+"000";

            SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",new Locale("en"));

            String sd = sdf.format(new Date(Long.parseLong(beginDate)));
			map.put("id", firstObj.getString("id"));
            map.put("resource", "QQ");
            map.put("created_at", sd);
            map.put("userscreen_name", firstObj.getString("nick"));
            map.put("source",">"+firstObj.getString("from")+"</a>");
            map.put("text", firstObj.getString("text"));
            map.put("reposts_count",firstObj.getString("count"));
    		map.put("comments_count",firstObj.getString("mcount"));
    		map.put("attitudes_count","0");
    		map.put("userprofile_image_url",firstObj.getString("head")+"/40");
			listArrays.add(map);
			return listArrays;
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        return null;
	}
	
	public static List<HashMap<String, String>> userJsonToHasArr(ModelResult result)
	{
		if(!result.isSuccess())
		{
			return null;
		}
		
        try {
        	JSONObject object =(JSONObject) result.getObj();
			List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map =new HashMap<String, String>();
			JSONObject firstObj =object.getJSONObject("data");
			map.put("name", firstObj.getString("nick"));
			map.put("followers_count", firstObj.getString("fansnum"));
			map.put("friends_count", firstObj.getString("idolnum"));
			map.put("statuses_count", firstObj.getString("tweetnum"));
			map.put("avatar_large", firstObj.getString("head")+"/100");
			listArrays.add(map);
			return listArrays;
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        return null;
	}
	public static List<HashMap<String, String>> timeLineJsonToHasArr(RennResponse response)
   {
		 List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
		try{
	         JSONArray jsonArrays =response.getResponseArray();
          
          
           for (int i = 0; i < jsonArrays.length(); i++) {  
               JSONObject jsonObject = jsonArrays.getJSONObject(i);  
               HashMap<String, String> map = new HashMap<String, String>(); 

               SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",new Locale("en"));
               SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm",new Locale("en"));
               String sd = sdf.format(sdf1.parse(jsonObject.getString("time")));
              /* if(jsonObject.getInt("type")==2)
               {
            	   map.put("retweeted_statususerscreen_name", jsonObject.getJSONObject("source").getString("nick"));
            	   map.put("retweeted_statustext", jsonObject.getJSONObject("source").getString("text"));
            	   map.put("retweeted_statusid", jsonObject.getJSONObject("source").getString("id"));
               }*/
               map.put("id", jsonObject.getJSONObject("resource").getString("id"));
               map.put("resource", "Renn");
               map.put("created_at", sd);
               map.put("userscreen_name", jsonObject.getJSONObject("sourceUser").getString("name"));
               map.put("userid", jsonObject.getJSONObject("sourceUser").getString("id"));
               if(jsonObject.getString("source")!="null")
               map.put("source",">"+jsonObject.getJSONObject("source").getString("text")+"</a>");
               else
               map.put("source", "> </a>");
               if(jsonObject.getString("thumbnailUrl")!="null")
                   map.put("images",">"+jsonObject.getString("thumbnailUrl"));
                   else
                   map.put("images", "> </a>");
               map.put("text", jsonObject.getJSONObject("resource").getString("content"));
               map.put("reposts_count","0");
       		map.put("comments_count","0");
       		map.put("attitudes_count","0");
       		Date sDate=sdf1.parse(jsonObject.getString("time"));
       		map.put("timestamp",sDate.getTime()+"");
       		map.put("userprofile_image_url",jsonObject.getJSONObject("sourceUser").getJSONArray("avatar").getJSONObject(0).getString("url"));
               listArrays.add(map);  
               
           }  
         
       } catch (JSONException e) {  
           // TODO Auto-generated catch block  
           System.out.println(e.getMessage());  
       } catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
		  return listArrays;
   }
   
 
  
   
   public static HashMap<String, String> JsonToHash(String json)
	{
		 JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(json);
			Iterator<String> itKeys = jsonObject.keys();
	         HashMap<String, String> map = new HashMap<String, String>();  
	         String key="";
	         while(itKeys.hasNext())
	         {
	      	   key=(String)itKeys.next();
	      	   map.put(key,jsonObject.getString(key));
	         }
	         return map;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
         return null;
	}
   
   public static void JsonAddToHash(HashMap<String, String> map, String json)
 	{
 		 JSONObject jsonObject;
 		try {
 			jsonObject = new JSONObject(json);
 			Iterator<String> itKeys = jsonObject.keys(); 
 	         String key="";
 	         while(itKeys.hasNext())
 	         {
 	      	   key=(String)itKeys.next();
 	      	   map.put("user"+key,jsonObject.getString(key));
 	         }
 		} catch (JSONException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} 
 	}
   public static boolean isJson(String value) {  
       try {  
           new JSONObject(value);  
       } catch (JSONException e) {  
           return false;  
       }  
       return true;  
   }  
}



